home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n3.zip / BEGINNER.ZIP / HELLO.C next >
C/C++ Source or Header  |  1993-02-15  |  7KB  |  208 lines

  1. /*============================================================================
  2.   Hello - Windows Programmer's Journal Volume 1 Number 3
  3.  
  4.   File      : Hello.C
  5.  
  6.   Prototype : 
  7.  
  8.   Call      : 
  9.  
  10.   Library   : 
  11.  
  12.   Example   : 
  13.  
  14.   7 February 1993 - dlcampbell
  15.   1993 Dave Campbell WynApse
  16.  
  17.   from:
  18.        Hello.C
  19.     By Pete Davis and Mike Wallace
  20.     Windows Programmer's Journal
  21.     Volume 1 Number 2
  22. ----------------------------------------------------------------------------*/
  23. #include <windows.h>
  24. #include "hello.h"
  25.  
  26. char   szAppName[] = "Hello";
  27. HANDLE hInst;
  28. HWND   hWndMain;
  29. int    InitSettings;
  30.  
  31. /*----------------------------------------------------------------------------
  32.   Function Prototypes
  33. ----------------------------------------------------------------------------*/
  34. BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  35. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  36. BOOL            InitInstance(HANDLE);
  37.  
  38. /* The main procedure. This is called by Windows when your program is run. */
  39. /* hInstance is the handle for the current instance of the program         */
  40. /* hPrevInst is the handle for the last instance of the program to run     */
  41. /* CmdLine is a pointer to a string of whatever command line params came in*/
  42. /* nCmd is the specifies the applications appearance.                      */
  43.  
  44. int FAR PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInst, LPSTR CmdLine,
  45.                                                                      int nCmd)
  46. {
  47. MSG   msg;                                /* Message */
  48. HWND  hWnd;                       /* Our Window Handle */
  49. HMENU hMenu;
  50.  
  51. if (!hPrevInst)                         /* If no instance already exists, */
  52.    {
  53.     if (!InitInstance(hInstance))       /* try initializing instance */
  54.         return FALSE;                     /* No dice, it failed */
  55.     }
  56.  
  57. hInst = hInstance;
  58.     
  59. hWndMain = CreateWindow(szAppName,              /* Window Class */
  60.                               "Hello World Program",  /* Window caption */
  61.                               WS_OVERLAPPEDWINDOW,    /* Overlapped style */
  62.                               CW_USEDEFAULT,          /* Use defaults for the */
  63.                               CW_USEDEFAULT,          /* X & Y Positions and  */
  64.                               CW_USEDEFAULT,          /* X & Y sizes */
  65.                               CW_USEDEFAULT,          
  66.                               NULL,                   /* Parent Window */
  67.                               NULL,
  68.                               hInstance,              /* Instance */
  69.                               NULL);                  /* Creation Params */
  70.  
  71. if (hWndMain == NULL)
  72.    return 0;
  73.  
  74. hMenu = GetSystemMenu(hWndMain, FALSE);
  75. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  76. AppendMenu(hMenu, MF_STRING,    IDM_SETUP, "Setup...");
  77.  
  78. ShowWindow(hWndMain, nCmd);
  79. UpdateWindow(hWndMain);
  80.     
  81. while (GetMessage(&msg, NULL, 0, 0))
  82.    {
  83.     TranslateMessage(&msg);
  84.     DispatchMessage(&msg);
  85.    }
  86.     
  87. return msg.wParam;
  88. }                                       /* int FAR PASCAL WinMain */
  89.  
  90. BOOL InitInstance (HANDLE hInstance)
  91. {
  92. WNDCLASS wc;                            /* Window class structure */
  93.  
  94. wc.lpszMenuName  = NULL;
  95. wc.lpszClassName = szAppName;
  96. wc.hbrBackground = GetStockObject(WHITE_BRUSH);  /* White bkgrnd */
  97. wc.hInstance     = hInstance;
  98. wc.style         = CS_VREDRAW | CS_HREDRAW;
  99. wc.lpfnWndProc   = WndProc;              /* Name of proc to handle window */
  100. wc.hCursor       = LoadCursor(NULL, IDC_ARROW);  /* Arrow Cursor */
  101. wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);  /* Generic Icon */
  102. wc.cbClsExtra    = 0;                /* no extras */
  103. wc.cbWndExtra    = 0;                /* No Extras */
  104.  
  105. return (RegisterClass(&wc)); /* Let's register that class */
  106. }                                       /* BOOL InitInstance */
  107.  
  108. /* This is our main windows procedure. It receives messages in message, then,
  109. depending on what the message is, we react accordingly
  110. */
  111.  
  112. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  113. {
  114. HDC            hdc;
  115. PAINTSTRUCT    ps;
  116. RECT           rect;
  117. static FARPROC lpfnHelloDlgProc;
  118.  
  119. switch (message)
  120.    {
  121.    case WM_CREATE :
  122.       InitSettings = GetPrivateProfileInt("Hello", "Setup", 1, "Hello.ini");
  123.       InitSettings = InitSettings ? IDM_HELLO : IDM_GOODBYE;
  124.       hdc = GetDC(hWnd);
  125.          InvalidateRect(hWnd, NULL, TRUE);
  126.       ReleaseDC(hWnd, hdc);
  127.  
  128. /*----------------------------------------------------------------------------
  129.   fall through to WM_PAINT...
  130. ----------------------------------------------------------------------------*/
  131.    case WM_PAINT :
  132.       hdc = BeginPaint(hWnd,&ps);        /* returns pointer to hdc */
  133.       GetClientRect(hWnd, &rect);
  134. /*
  135.   -1 tells the DrawText function to calculate length of string based on
  136.   NULL-termination
  137. */
  138.       DrawText(hdc, (InitSettings == IDM_HELLO) ? "Hello Windows!" : "Goodbye Windows!", -1,
  139.                                    &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  140.       EndPaint(hWnd,&ps);
  141.       return 0;
  142.  
  143.    case WM_SYSCOMMAND :
  144.       switch (wParam)
  145.          {
  146.          case IDM_SETUP :
  147.             lpfnHelloDlgProc = MakeProcInstance(HelloDlgProc, hInst);
  148.             DialogBox(hInst, "Hello", hWnd, lpfnHelloDlgProc);
  149.             FreeProcInstance(lpfnHelloDlgProc);
  150.             return 0;
  151.          }
  152.  
  153.       break;
  154.  
  155.   case WM_DESTROY :
  156.       PostQuitMessage(0);
  157.       return 0;
  158.    }
  159. return DefWindowProc(hWnd, message, wParam, lParam);
  160. }                                       /* long FAR PASCAL WndProc */
  161.  
  162. /*============================================================================
  163.   HelloDlgProc -- 
  164.  
  165.   File      : Test.C
  166.  
  167.   Prototype : BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  168.  
  169.   Call      : HelloDlgProc(hDlg, message, wParam, lParam);
  170.  
  171.   Library   : 
  172.  
  173.   19 January 1993 - dlcampbell
  174.   (c) 1993 WynApse
  175. ----------------------------------------------------------------------------*/
  176. BOOL FAR PASCAL HelloDlgProc (HWND hDlg, WORD message, WORD wParam,
  177.                                                                   LONG lParam)
  178. {
  179. switch (message)
  180.    {
  181.    case WM_INITDIALOG :
  182.       CheckRadioButton(hDlg, IDM_HELLO, IDM_GOODBYE, InitSettings);
  183.       return TRUE;
  184.  
  185.    case WM_COMMAND :
  186.       switch (wParam)
  187.          {
  188.          case IDM_HELLO : 
  189.                WritePrivateProfileString("Hello", "Setup", "1", "Hello.ini");
  190.             InitSettings = wParam;
  191.             break;
  192.  
  193.          case IDM_GOODBYE : 
  194.                WritePrivateProfileString("Hello", "Setup", "0", "Hello.ini");
  195.             InitSettings = wParam;
  196.             break;
  197.  
  198.          case IDOK :
  199.             EndDialog(hDlg, wParam);
  200.             return TRUE;
  201.  
  202.          }
  203.       break;
  204.  
  205.    }
  206. return FALSE;
  207. }                                       /* HelloDlgProc */
  208.